//
//  LiveSqlite.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/22.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "LiveSqlite.h"

static FMDatabase *_db;

@implementation LiveSqlite

/*
 SQLite将数据划分为以下几种存储类型：
 integer : 整型值
 real : 浮点值
 text : 文本字符串
 blob : 二进制数据（比如文件
 */

+ (void)initialize
{
    // 打开数据库
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"live.sqlite"];
    _db = [FMDatabase databaseWithPath:path];
    [_db open];
    NSLog(@"数据库路径====%@",path);
    
    // 创表
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_attentionMatch (title text PRIMARY KEY, home_team text , home_logo text, keywords text, label text, sdate text, time text, type text, url text, visit_logo text, visit_team text, matchInfo text, matchProperty text, focus text, matching text);"];
}

+ (NSMutableArray *)matchModel
{
    // 得到结果集
    FMResultSet *set = [_db executeQuery:@"SELECT * FROM t_attentionMatch;"];
    NSMutableArray *matchArray = [[NSMutableArray alloc] init];
    while (set.next) {
        ListArrayModel *model = [[ListArrayModel alloc] init];
        model.title = [set stringForColumn:@"title"];
        model.home_team = [set stringForColumn:@"home_team"];
        model.home_logo = [set stringForColumn:@"home_logo"];
        model.keywords = [set stringForColumn:@"keywords"];
        model.label = [set stringForColumn:@"label"];
        model.sdate = [set stringForColumn:@"sdate"];
        model.time = [set stringForColumn:@"time"];
        model.type = [set stringForColumn:@"type"];
        model.url = [set stringForColumn:@"url"];
        model.visit_logo = [set stringForColumn:@"visit_logo"];
        model.visit_team = [set stringForColumn:@"visit_team"];
        model.matchInfo = [set stringForColumn:@"matchInfo"];
        model.matchProperty = [set stringForColumn:@"matchProperty"];
//        model.focus = [[set stringForColumn:@"focus"]boolValue];
//        model.matching = [[set stringForColumn:@"matching"]boolValue];
        model.focus = [set boolForColumn:@"focus"];
        model.matching = [set boolForColumn:@"matching"];
        [matchArray addObject:model];
    }
    return matchArray;
}

+ (void)addMatchWithModel:(ListArrayModel *)model
{
    NSString *focusStr =[NSString stringWithFormat:@"%d",model.focus];
    NSString *matchingSrr =[NSString stringWithFormat:@"%d",model.matching];
    
    [_db executeUpdateWithFormat:@"INSERT INTO t_attentionMatch(title, home_team, home_logo, keywords, label, sdate, time, type, url, visit_logo,visit_team, matchInfo, matchProperty,focus,matching) VALUES (%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@);",model.title,model.home_team,model.home_logo,model.keywords,model.label,model.sdate,model.time,model.type,model.url,model.visit_logo,model.visit_team, model.matchInfo,model.matchProperty,focusStr,matchingSrr];
}

+ (void)deleteMatchWithModel:(ListArrayModel *)model
{
    [_db  executeUpdate:@"delete from t_attentionMatch  where  title  = ?", model.title];
}



@end
